import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class CouponSeckill {
    
    private static final AtomicInteger couponCount = new AtomicInteger(5);

    static class Customer {
        private final String name;
        public Customer(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }

    static class SeckillTask implements Callable<String> {
        private final Customer customer;

        public SeckillTask(Customer customer) {
            this.customer = customer;
        }

        @Override
        public String call() {
            int current = couponCount.getAndDecrement();
            if (current > 0) {
                return customer.getName() + " 抢到了优惠券！剩余优惠券数量：" + (current - 1);
            } else {
                // 如果减到负数，需要回滚（仅在初始值为0时可能发生）
                couponCount.incrementAndGet();
                return customer.getName() + " 没有抢到优惠券，优惠券已抢完！";
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        int customerCount = 10;
        CountDownLatch latch = new CountDownLatch(customerCount);

        for (int i = 0; i < customerCount; i++) {
            Customer customer = new Customer("顾客" + (i + 1));
            executor.submit(() -> {
                try {
                    String result = new SeckillTask(customer).call();
                    System.out.println(result);
                } finally {
                    latch.countDown();
                }
            });
        }

        latch.await();
        executor.shutdown();
    }
}